<template>
	<view class="main">
		<view class="tip"> 请横屏签名 </view>
		<view class="canvas">
			<view class="left">
				<canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"></canvas>
			</view>
			<view class="right">
				<view class="reset vrl" @click="clear">
					<span class="span">重</span>
					<span class="span">置 </span>
				</view>
				<view class="r_tip vrl">
					<span class="span">请</span>
					<span class="span">横</span>
					<span class="span">屏</span>
					<span class="span">签</span>
					<span class="span">名</span>

				</view>
				<view class="confirm vrl" @click="finish">
					<span class="span">确</span>
					<span class="span">定</span>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "drawingboard",
		data() {
			return {
				ctx: '', //绘图图像
				points: [] //路径点集合
			};
		},
		created() {
			this.ctx = uni.createCanvasContext("mycanvas", this); //创建绘图对象
			//设置画笔样式
			this.ctx.lineWidth = 2;
			this.ctx.lineCap = "round"
			this.ctx.lineJoin = "round"
		},
		methods: {
			//触摸开始，获取到起点
			touchstart: function(e) {
				let startX = e.changedTouches[0].x;
				let startY = e.changedTouches[0].y;
				let startPoint = {
					X: startX,
					Y: startY
				};

				/* **************************************************
				    #由于uni对canvas的实现有所不同，这里需要把起点存起来
				 * **************************************************/
				this.points.push(startPoint);

				//每次触摸开始，开启新的路径
				this.ctx.beginPath();
			},
			//触摸移动，获取到路径点
			touchmove: function(e) {
				let moveX = e.changedTouches[0].x;
				let moveY = e.changedTouches[0].y;
				let movePoint = {
					X: moveX,
					Y: moveY
				};
				this.points.push(movePoint); //存点
				let len = this.points.length;
				if (len >= 2) {
					this.draw(); //绘制路径
				}
			},
			// 触摸结束，将未绘制的点清空防止对后续路径产生干扰
			touchend: function() {
				this.points = [];
			},
			/* ***********************************************
			#   绘制笔迹
			#    1.为保证笔迹实时显示，必须在移动的同时绘制笔迹
			#    2.为保证笔迹连续，每次从路径集合中区两个点作为起点（moveTo）和终点(lineTo)
			#    3.将上一次的终点作为下一次绘制的起点（即清除第一个点）
			************************************************ */
			draw: function() {
				let point1 = this.points[0]
				let point2 = this.points[1]
				this.points.shift()
				this.ctx.moveTo(point1.X, point1.Y)
				this.ctx.lineTo(point2.X, point2.Y)
				this.ctx.stroke()
				this.ctx.draw(true)
			},
			//清空画布
			clear: function() {
				let that = this;
				uni.getSystemInfo({
					success: function(res) {
						let canvasw = res.windowWidth;
						let canvash = res.windowHeight;
						that.ctx.clearRect(0, 0, canvasw, canvash);
						that.ctx.draw(true);
					},
				})
			},
			// 点击确定按钮，获取canvas的宽度高度
			finish() {
				uni.createSelectorQuery().in(this).select(".mycanvas").fields({
					size: true,
					scrollOffset: true
				}, data => {
					this.getImg(data.width, data.height);
				}).exec();


			},
			// 判断是否有绘画
			getImg(w, h) {
				uni.canvasGetImageData({
					canvasId: 'mycanvas',
					x: 0,
					y: 0,
					width: w,
					height: h,
					success: (res) => {
						let content = res.data.some((res) => {
							return res != 0
						});
						if (content) {
							this.submit();
						} else {
							uni.showModal({
								title: '请签名！'
							})
						}
					},
					fail: (err) => {}
				}, this)
			},

			//完成绘画并保存数据
			submit: function() {
				uni.canvasToTempFilePath({
					canvasId: 'mycanvas',
					success: (res) => {
						let path = res.tempFilePath;
						this.$emit('success', path)
					},
					fail: (err) => {
						console.log(err);
					}
				}, this)
			}
		},
	}
</script>

<style scoped>
	.main {
		padding: 20rpx;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
		box-sizing: border-box;
		width: 100%;
		height: 100vh;
	}

	.tip {
		width: 100%;
		height: 40rpx;
		line-height: 40rpx;
		font-size: 20rpx;
		text-align: center;
		letter-spacing: 20rpx;
	}

	.canvas {
		display: flex;
		width: 100%;
		height: calc(100% - 40rpx);
	}

	.left {
		flex: 1;
		flex-shrink: 0;
		border: 1rpx dashed #C0C0C0;
		border-radius: 20rpx;
		overflow: hidden;
	}

	.mycanvas {
		width: 100%;
		height: 100%;
		background-color: #f6f6f6;
	}

	.right {
		width: 60rpx;
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		align-items: flex-end;
	}

	.right .vrl {
		writing-mode: vertical-rl;
	}

	.vrl .span {
		display: inline-block;
		transform: rotate(90deg);
	}

	.right .reset {
		width: 50rpx;
		height: 120rpx;
		border-radius: 10rpx;
		background-color: #f9ae3d;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		letter-spacing: 8rpx;
	}

	.right .confirm {
		width: 50rpx;
		height: 120rpx;
		border-radius: 10rpx;
		background-color: #5ac725;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		letter-spacing: 8rpx;
	}

	.right .r_tip {
		width: 50rpx;
		font-size: 20rpx;
		letter-spacing: 20rpx;
		line-height: 50rpx;
	}
</style>
